#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>

typedef struct ListNode
{
	int val;
	struct ListNode* next;
}ListNode;

ListNode* BuyNode(int x)
{
	ListNode* node = (ListNode*)malloc(sizeof(ListNode));
	if (node == NULL)
	{
		exit(1);
	}
	node->val = x;
	node->next = NULL;
	return node;
}

ListNode* CreateCircle(int m)
{
	ListNode* phead = BuyNode(1);
	ListNode* ptail = phead;
	for (int i = 2; i <= m; i++)
	{
		ptail->next = BuyNode(i);
		ptail = ptail->next;
	}
	ptail->next = phead;
	return ptail;
}

int ysf(int m, int n)
{
	ListNode* prev = CreateCircle(m);
	ListNode* pcur = prev->next;
	int count = 1;
	while (pcur->next != pcur)
	{
		if (count = n)
		{
			prev->next = pcur->next;
			free(pcur);
			pcur = prev->next;
			count = 1;
		}
		else
		{
			prev = pcur;
			pcur = pcur->next;
			count++;
		}
	}
	return pcur->val;
}
